<template>
  <div>
    <div class="title">
      全选:<input type="checkbox" :checked="isAllChecked" @click="changeFn" />
      <button @click="fanxuanFn">反选</button>
      <br />
      <ul>
        <li v-for="(obj, index) in arr" :key="index">
          <input type="checkbox" v-model="obj.c" />
          {{ obj.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        {
          name: "猪八戒",
          c: false,
        },
        {
          name: "孙悟空",
          c: false,
        },
        {
          name: "唐僧",
          c: false,
        },
        {
          name: "白龙马",
          c: false,
        },
      ],
    };
  },
  methods: {
    changeFn(e) {
        this.arr.forEach(item =>{return item.c=e.target.checked})
    },
    fanxuanFn(){
        this.arr.forEach(item =>{return item.c=!item.c})
    }
  },
  computed: {
    isAllChecked() {
      return this.arr.every((item) => item.c);
    },
  },
};
</script>

<style>
</style>